package com.wangdao.wechatmall.service.admin;


import com.github.pagehelper.PageInfo;
import com.wangdao.wechatmall.bean.bo.admin.HistoryListBO;
import com.wangdao.wechatmall.bean.pojo.SearchHistory;
import com.wangdao.wechatmall.bean.pojo.SearchHistoryExample;
import com.wangdao.wechatmall.bean.vo.admin.BaseDataVO;
import com.wangdao.wechatmall.mapper.SearchHistoryMapper;
import com.wangdao.wechatmall.util.MyPageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ZhangXiao on 2021/7/9
 */
@Service
public class HistoryServiceImpl implements HistoryService {

    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Override
    public BaseDataVO<SearchHistory> querySearchHistory(HistoryListBO historyListBO) {
        MyPageHelper.startPage(historyListBO);
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();

        Integer userId = historyListBO.getUserId();
        String keyword = historyListBO.getKeyword();
        if (userId != null && userId != ' ') {
            criteria.andUserIdEqualTo(userId);
        }
        if (keyword != null && !"".equals(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }

        searchHistoryExample.setOrderByClause(historyListBO.getSort() + " " + historyListBO.getOrder());

        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);

        PageInfo<SearchHistory> searchHistoryPageInfo = new PageInfo<>(searchHistories);
        long total = searchHistoryPageInfo.getTotal();

        return BaseDataVO.ok(searchHistories, total);
    }
}
